﻿@using Topro.Entity.Enum;
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="easyui-layout case" data-options="fit: true">
    <div data-options="region: 'center', border: false" style="padding:10px; overflow: hidden;">
        <div class="easyui-layout" data-options="fit: true">
            <div class="easyui-layout" data-options="fit: true">
                <!--header-->
                <div data-options="region:'north',border: false,split:true" style="height:40px;">
                    <div class="data-tools clearfix">
                        <div class="pull-left form-inline ">
                            <a class="btn btn-default btn-sm" data-modal="/Product/ProductAddOrEdit" data-title="新增" data-width="452" data-height="520"><span class="glyphicon glyphicon-plus"></span> 新增</a>
                        </div>
                        <div class="pull-right">
                            <form class="form-inline" id="searchForm">
                                <div class="input-group input-group-sm">
                                    <label class="input-group-addon m5_l">名称：</label>
                                    <input class="form-control" name="name" type="text" autocomplete="off">
                                </div>

                                <div class="input-group input-group-sm">
                                    <label class="input-group-addon m5_l">状态：</label>
                                    <select class="form-control wd80">
                                        <option value="禁用">禁用</option>
                                        <option value="启用">启用</option>
                                    </select>
                                </div>

                                <button type="button" class="btn btn-default btn-sm active search"><span class="glyphicon glyphicon-search"></span> 搜索</button>
                            </form>
                        </div>
                    </div>
                </div>
                <div data-options="region:'center',split: false, border: false">
                    <!-- 表格数据 -->
                    <table id="stockInDG"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    var stockInDG = $('#stockInDG');

    $(function () {
        //审核
        $(".add").click(function(){
            page.add();
        });
        //审核
        $(".createOrder").click(function () {
            Topro.DAO.CreateOrder({
                title: '创建入库单',
                isSingle: true,
                selectingData: [],
                saveCallBack: function (rows) {

                }
            });
        });
        //搜索
        $(".search").click(function(){
            page.search();
        });

        page.doSearch();
    });

    function Tip_success(word) {
        Topro.UI.Tip_success(word);
    }

    var page = {
        view: function (obj) {
            Topro.UI.DialogOpen($(obj).data("href") + '?v=' + Math.random().toString(36).substr(2, 4), {
                title: $(obj).data("title"),
                width: $(obj).data("width"),
                height: $(obj).data("height")
            });
        },
        search: function () {
            //表单
            var searchform = $("#searchForm").serializeArray();
            //表单转为Json
            var queryParams = page.getJson(searchform);

            stockInDG.datagrid({
                queryParams: queryParams
            });

            return false;
        },
        getJson: function (array) {
            if (array != undefined && array.length > 0) {
                var json = {};
                $(array).each(function (i, v) {
                    json[v.name] = v.value;
                });
                return json;
            } else {
                return {};
            }
        },
        queryParams:function(){
            var params = {
            };

            return params;
        },
        createOrder: function () {
            Topro.DAO.CreateOrder({
                title: '创建入库单',
                isSingle: true,
                selectingData: [],
                saveCallBack: function (rows) {
                }
            });
        },
        add:function(){
            var dialog = Topro.UI.ShowDialog({
                title: '新增',
                href: '/StockInOut/StockInAddOrEdit',
                width: 500,
                height: 350,
                onSave: function (res) {
                    //表单验证
                    var isValid = window.$("#productFromValidate").form('validate');
                    if (!isValid) {
                        return isValid;
                    }

                    //表单提交
                    window.$('#productFromValidate').form('submit', {
                        url: '/Product/Save',
                        success: function (data) {
                            if (data.success) {
                                datagrid.datagrid('reload');

                                Topro.UI.Prompt({ content: data.message, type: 'info' });

                                dialog.dialog("destroy");
                            } else {
                                Ims.UI.Prompt({ content: data.message, type: 'error' });
                                return false;
                            }
                        }
                    });

                    return false;
                },
                //  点击应用按钮触发的事件，如果该事件范围 false，则点击应用后该按钮不被自动禁用。
                onApply: function () {
                },
                //  关闭窗口时应触发的事件，easyui-dialog本身就有
                onClose: function () {

                }
            });
        },
        delete:function(){
            var row = datagrid.datagrid('getSelected');
            if(row){
                var rowIndex = datagrid.datagrid('getRowIndex', row);
                if(rowIndex<0){
                    window.top.$.messager.alert('提示', '请选择删除行.', 'info');
                }

                datagrid.datagrid('deleteRow',rowIndex);
            }else{
                window.top.$.messager.alert('提示', '请选择删除行.', 'info');
                return;
            }
        },
        approval: function (obj) {

            var data = {
                id:$(obj).data("id")
            };

            var msg = $(obj).data("msg");

            Topro.UI.Confirm({
                Msg: msg, Callback: function (result) {
                    if (result) {
                        Ims.UI.Ajax({
                            type: "POST",
                            url: "/StockInOut/StockInApproval",
                            data: data,
                            success: function (res) {
                                if (res.success) {

                                    datagrid.datagrid('reload');

                                    Ims.UI.Tip_success(res.message);

                                } else {
                                    Ims.UI.Tip_warning("操作失败！" + res.message);
                                }
                            },
                            error: function (e) {
                                Topro.UI.Tip_warning("操作失败！请查看网络，请重试。");
                            }
                        });
                    }
            } });
        },
        selectProduct: function () {
            Topro.DAO.Product({
                title: '物料',
                isSingle: true,
                saveCallBack: function(rows) {

                    if(rows.length<=0){
                        window.top.$.messager.alert('提示', '请选择物料！', 'info');
                        return;
                    }else{
                        $.each(rows, function(i, item){
                            datagrid.datagrid('insertRow',{
	                            index: 0,
	                            row: item
                            })
                        });
                    }
                }
            });
        },
        doSearch: function () {

            //固定行
            var columns = [
                [
                    {
                        field: 'name',
                        title: '名称',
                        width: 170,
                        align: 'left',
                    },
                    {
                        field: 'type',
                        title: '类型 ',
                        width: 120,
                        align: 'center',
                        formatter: function (value, row, index) {
                            var result = "";

                            if (!Topro.Util.isNullOrEmptySpance(value)) {
                                result = value;
                            }
                            return result;
                        }
                    },
                    {
                        field: 'code',
                        title: '编码',
                        width: 80,
                        align: 'left',
                    },
                    {
                        field: 'spec',
                        title: '规格 ',
                        width: 150,
                        align: 'left'
                    },
                    {
                        field: 'length',
                        title: '长 ',
                        width: 75,
                        align: 'center',
                    },
                    {
                        field: 'width',
                        title: '宽 ',
                        width: 75,
                        align: 'center',
                    },
                    {
                        field: 'stationNo',
                        title: '高 ',
                        width: 75,
                        align: 'center',
                    },
                    {
                        field: 'status',
                        title: '状态',
                        align: 'center',
                        width: 120,
                        formatter: function (value, row, index) {

                            var result = "";

                            if (value == @((sbyte)ProductStatus.启用)) {
                                result = "<span class='green bold'>启用</span>";
                            } else if (value == @((sbyte)ProductStatus.禁用)) {
                                result = "<span class='red bold'>禁用</span>";
                            }

                            return result;

                        }
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        align: 'left',
                        width: 200
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        align: 'center',
                        width: 140,
                        formatter: function (value, row, index) {

                            var result = value.replace("T"," ");

                            return result;
                        }
                    },
                    {
                        field: 'id',
                        title: '操作',
                        width: 120,
                        align: 'center',
                        formatter: function (value, row, index) {

                            var result = "";

                            if (row.status == @((sbyte)ProductStatus.启用)) {
                                result += "<a class=\"btn btn-danger btn-xs\" data-confirm=\"确定要禁用该权限？\" data-succesFun=\"page.search()\" data-href=\"/Product/UpdateStatus?id={0}&status={1}\" onclick=\"return Topro.UI.DialogConfirm(this)\">禁用</a>".format(row.id,@((sbyte)ProductStatus.禁用));
                            } else if (row.status == @((sbyte)ProductStatus.禁用)) {
                                result += "<a class=\"btn btn-primary btn-xs\" data-confirm=\"确定要启用该权限？\" data-succesFun=\"page.search()\" data-href=\"/Product/UpdateStatus?id={0}&status={1}\" onclick=\"return Topro.UI.DialogConfirm(this)\">启用</a>".format(row.id,@((sbyte)ProductStatus.启用));
                                result += "<button type=\"button\" class=\"btn btn-default btn-xs m5_l\" data-href=\"/Product/ProductAddOrEdit/{0}\" data-title=\"编辑\" data-width=\"400\" data-height=\"520\" onclick=\"return Topro.UI.View(this)\"><span class=\"glyphicon glyphicon-edit\"></span> 编辑</button>".format(row.id);
                            }

                            return result;
                        }
                    }
                ]
            ];

            Topro.UI.DataGrid(stockInDG, {
                fit: true,
                method: "post",
                url: "@Url.Action("ProductQuery", "Product")",
                queryParams: page.queryParams(),
                idField: 'id',
                pagination: true,
                singleSelect: true,
                rownumbers: true,
                showFooter: true,
                autoEditing:true,
                frozenColumns: null,
                columns: columns
            });
        }
    }
</script>